package com.tjbank.cssys.web.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CreditStopNoticePerson;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CreditStopNoticePersonService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

@Api(tags = "CkLevelController", description = "失联名单库")
@RestController
@RequestMapping("/stop/notice/person")
public class CreditStopNoticePersonController {

    @Autowired
    private CkLogService ckLogService;

    @Autowired
    private CreditStopNoticePersonService creditStopNoticePersonService;

    private final static Logger logger = LoggerFactory.getLogger(CreditStopNoticePersonController.class);

    @ApiOperation("获取信用卡失联数据")
    @PostMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "1" ,required = true) Integer page , @RequestParam(defaultValue = "5",required = true) Integer pageSize,
                             CreditStopNoticePerson creditStopNoticePerson, HttpServletRequest request) {
        PageInfo<CreditStopNoticePerson> creditStopNoticePeople = creditStopNoticePersonService.selectList(page,pageSize , creditStopNoticePerson);
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        if(creditStopNoticePeople!=null) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams("");
            ckLog.setOperationType("查询信用卡失联页面");
            ckLog.setResult("success:200");
            logger.info("获取信用卡失联数据，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(creditStopNoticePeople, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }



    @ApiOperation("录入失联数据")
    @PostMapping("/add")
    public CommonResult addInsert(@RequestBody CreditStopNoticePerson creditStop, HttpServletRequest request) {
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        logger.info("开始录入失联数据");

        int insert = creditStopNoticePersonService.insert(creditStop);
        if (insert != 0) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(JSON.toJSONString(creditStop));
            ckLog.setOperationType("录入失联数据");
            ckLog.setResult("success:200");
            logger.info("录入失联数据结束，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(insert, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }


    @ApiOperation("出库")
    @PostMapping("/delete")
    public CommonResult delete(CreditStopNoticePerson crestop, HttpServletRequest request) {
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {

        }
        logger.info("逻辑删除失联数据开始~~");
        int i = creditStopNoticePersonService.UpdateStatus(crestop);
        if (String.valueOf(i) != null) {
            CkLog ckLog = new CkLog();
            ckLog.setUserId(userCode);
            ckLog.setLogId(UUID.randomUUID().toString());
            ckLog.setTimes(new Date());
            ckLog.setParams(String.valueOf(crestop));
            ckLog.setOperationType("逻辑删除失联数据");
            ckLog.setResult("success:200");
            logger.info("逻辑删除失联数据，添加到日志表中");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(i, "操作成功");
        } else {
            CkLog ckLog = new CkLog();
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("操作有误！");
        }
    }

}
